<?php

namespace app\api\controller;

use app\admin\model\News;
use app\common\controller\Api;
use app\admin\model\Address as AddressModel;

/**
 * 收货地址接口
 */
class Address extends Api
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';

    /*
     * 收货地址显示
     * */
    public function index()
    {
        $model = new AddressModel();
        $data = $model::where(['status' => 1, 'user_id' => $this->auth->id])
            ->field('id,phone,name,city,address,is_default')
            ->select();
        $this->success('请求成功', $data, 1);
    }

    /*
     * 添加收货地址
     * */
    public function addAddress()
    {
        $model = new AddressModel();
        $data = input('post.');
        //如果接收到了is_default为空，就修改为-1，isset判断有没有值
        if (isset($data['is_default']) && $data['is_default'] == 1) {
            $model->where('user_id', 1)->update($data['is_default'] == -1);
        }

        $model->user_id = $this->auth->id;
        $msg = $this->validate($data, 'Address.add');
        if ($msg != true) $this->error($msg, [], 500);
        if ($model->save($data)) {
            $this->success('保存成功！', $data, 1);
        }
        $this->error('保存失败', $data, 500);
    }

    /*
     * 编辑收货地址详情
     * */
    public function edit()
    {
        $data = input('post.');
        if (empty($data['id'])) {
            $this->error('请选择要修改的地址！', [], 400);
        }
        $model = new AddressModel();
        $res = $this->validate($data, 'Address.edit');
        if ($res !== true) $this->error($res, [], 500);
        //  如果接收到了is_default并且不为空，更新这个用户所有的地址并把is_default设为-1在更新，
        if (isset($data['is_default']) && $data['is_default'] == 1) {
            $address = $model->where(['user_id' => $this->auth->id, 'is_default' => 1, 'status' => 1])->find();
            if (!empty($address)) {
                $address->is_default = -1;
                $address->save();
            }
            $model->user_id = $this->auth->id;
        }
        if ($model->update($data)) {
            $this->success('保存成功！', $data, 1);
        }
        $this->error('保存失败！', [], 500);
    }

    /*
     * 编辑收货地址
     * */
    public function editAddress()
    {
        $id = input('post.id/d');
        $model = new AddressModel();
        $data = $model::where(['is_default' => 1, 'status' => 1, 'user_id' => 1])->find();
        //若有$data==1,为默认地址
        if (!empty($data)) {
            $data->is_default = -1;
            $data->save();
        }
        //改为默认地址
        $edit = $model->where(['id' => $id])->update(['is_default' => 1]);
        if ($edit) {
            $this->success('修改成功', $data, 1);
        }
        $this->error('修改失败', [], 500);

    }



    /*
     * 删除收货地址
     * */
    public function deleteAddress()
    {
        $id = input('get.id/d');
        $model = new AddressModel();
        $data = $model::where(['id' => $id, 'status' => 1, 'user_id' => 1])->find();
        if (empty($data)) {
            $this->error('请选择删除的收货地址', [], 400);
        }
        if ($model::where(['id' => $id])->delete()) {
            $this->success('删除成功', [], 1);
        }
        $this->error('删除失败', [], 500);
    }
}